<%@codepage="65001"%>
<%session.codepage="65001"%>
<!-- #include virtual=/inc/dbConn.asp -->
<!-- #include virtual=/inc/function.asp -->
<!-- #include virtual=/inc/action.header.inc.asp -->

<%
	'## 파라미터 변수 선언부
	income		= Request("findIncome")
	accountCd	= Request("findAccountCd")
	compCd		= Request("findCompCd")
	part		= Request("findPart")
	mnth		= Request("findMnth")
	keyset		= Request("keyset")
	stDate		= Request("stDate")
	endDate		= Request("endDate")
	
	SQLTitle = "-- ■■■■ 회계(입금) 회사별 합계 그리드 List up ■■■■"
	
		If findArticle <> "" Then 
		addSql = " A.article LIKE '%" & findArticle & "%' "
	End If 
	If accountCd <> "" Then 
		If addSql <> "" Then addSql = addSql & " AND " End If 
		addSql = addSql & " A.accountCd = '" & accountCd & "' "
	End If 
	If compCd <> "" Then 
		If addSql <> "" Then addSql = addSql & " AND " End If 
		addSql = addSql & " A.compCd = '" & compCd & "' "
	End If 
	If part <> "" Then 
		If addSql <> "" Then addSql = addSql & " AND " End If 
		addSql = addSql & " A.part = '" & part & "' "
	End If 
	If keyset <> "" Then 
		If stDate <> "" Then 
			If addSql2 <> "" Then addSql2 = addSql2 & " AND " End If 
			addSql2 = addSql2 & keyset & " >= '" & stDate & "' "
			If addSql1 <> "" Then addSql1 = addSql1 & " AND " End If 
			addSql1 = addSql1 & " A.incomeDt >= '" & stDate & "' "
		End If 
		If endDate <> "" Then 
			If addSql2 <> "" Then addSql2 = addSql2 & " AND " End If 
			addSql2 = addSql2 & keyset & " <= '" & endDate & "' "
			If addSql1 <> "" Then addSql1 = addSql1 & " AND " End If 
			addSql1 = addSql1 & " A.incomeDt <= '" & endDate & "' "
		End If 
	End If 
	if mnth <> "" then
		stMnth = left(date(),4) & "-" & mnth
		If addSql <> "" Then addSql = addSql & " AND " End If 
		if keyset = "incomeDt" then 
			addSql = addSql & " A.incomeDt LIKE '" & stMnth & "%' "
		else
			addSql = addSql & " D." & keyset & " LIKE '" & stMnth & "%' "
		end if 		 
	end if 

	If addSql <> "" Then 
		addSql = " AND " & addSql
	End If 
	If addSql1 <> "" Then 
		addSql1 = " AND " & addSql1
	End If 
	If addSql2 <> "" Then 
		addSql2 = " AND " & addSql2
	End If 

	SQL = "SELECT SUM(amt) amt, SUM(orgAmt) orgAmt, SUM(taxAmt) taxAmt, SUM(incAmt) incAmt, company, compCd FROM ( " & _	
		  "		SELECT SUM(A.amount) amt, SUM(A.orgAmount) orgAmt, SUM(A.tax) taxAmt, 0 incAmt, C.company, C.compCd " & _
		  "			 FROM tblIncome A LEFT JOIN tblCompany C ON A.compCd=C.compCd " & _
		  "		WHERE A.flag='Y' " & addSql & addSql1 & _
		  "		GROUP BY C.company, C.compCd " & _
		  "		UNION ALL " & _
		  "		SELECT 0 amt, 0 orgAmt, 0 taxAmt, SUM(B.amount) incAmt, C.company, C.compCd FROM tblIncome A LEFT JOIN tblIncResult B ON  A.seq=B.incCd " & _
		  "				LEFT JOIN tblCompany C ON A.compCd=C.compCd " & _
		  "		WHERE A.flag='Y' AND B.flag='Y' " & addSql & addSql2 & _
		  "		GROUP BY C.company, A.amount, C.compCd " & _
		  ") P " & _
		  "GROUP BY company, compCd " & _
		  "ORDER BY company "

	If xMode="debug" Then 
		Call showDebug(SQLTitle,SQL) 
	End If 

	Set Rs=Server.CreateObject("ADODB.RecordSet")
	Rs.open SQL, DbCon, 1
	
	Response.write "<IncomeSum>" & vbcrlf
	total = 0
	totalOrgAmt = 0
	totalTaxAmt = 0
	If Rs.eof Or Rs.bof Then 
	Else 
		Do Until Rs.eof Or Rs.bof
			notInAmt = CDbl(rs("amt")) - CDbl(rs("incAmt"))
			Response.write "	<IncomeSumList>" & vbcrlf
				Call mkXMLNode("company", rs("company"), 2)
				Call mkXMLNode("compCd", rs("compCd"), 2)
				Call mkXMLNode("amount", FormatNumber(rs("amt"),0), 2)
				Call mkXMLNode("incAmt", FormatNumber(rs("incAmt"),0), 2)
				Call mkXMLNode("orgAmt", FormatNumber(rs("orgAmt"),0), 2)
				Call mkXMLNode("taxAmt", FormatNumber(rs("taxAmt"),0), 2)
				Call mkXMLNode("notInAmt", FormatNumber(notInAmt,0), 2)
			Response.write "	</IncomeSumList>" & vbcrlf
			total = total + CDbl(rs("amt"))
			totalOrgAmt = totalOrgAmt + CDbl(rs("orgAmt"))
			totalTaxAmt = totalTaxAmt + CDbl(rs("taxAmt"))
			totalIncAmt = totalIncAmt + CDbl(rs("incAmt"))
			totalNotInAmt = totalNotInAmt + CDbl(notInAmt)

		Rs.movenext
		Loop 
	End If
	Response.write "	<IncomeSumList>" & vbcrlf
		Call mkXMLNode("company", "<b>TOTAL</b>", 2)
		Call mkXMLNode("compCd", "", 2)
		Call mkXMLNode("amount", "<b>" & FormatNumber(total,0) & "</b>", 2)
		Call mkXMLNode("incAmt", "<b>" & FormatNumber(totalIncAmt,0) & "</b>", 2)
		Call mkXMLNode("orgAmt", "" & FormatNumber(totalOrgAmt,0) & "", 2)
		Call mkXMLNode("taxAmt", "<b>" & FormatNumber(totalTaxAmt,0) & "</b>", 2)
		Call mkXMLNode("notInAmt","<b>" & FormatNumber(totalNotInAmt,0) & "</b>", 2)
	Response.write "	</IncomeSumList>" & vbcrlf
	Response.write "</IncomeSum>" & vbcrlf
'	Call rsWithXML("Save OK!")
%>